package com.yyt.send.shedule;

import com.alibaba.fastjson.JSONObject;
import com.yyt.send.domain.SendMessage;
import com.yyt.send.dto.Send1DTO;
import com.yyt.send.repository.SendMessageRepository;
import com.yyt.send.service.SendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author yeyangtao created at 16:45 2020/9/14
 */
@Slf4j
@Configuration
@EnableScheduling
public class Send1 {
    @Resource
    private SendMessageRepository repository;
    @Resource
    private SendService sendService;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void sendMessage() {

        log.info("开始执行");
        SendMessage message = new SendMessage();
        try {
            Send1DTO dto = new Send1DTO();
            JSONObject result = sendService.send1("w", 1, dto);
            message = new SendMessage(result.toJSONString(), true);
            log.info("执行成功：" + result);
        } catch (Exception e) {
            log.error("执行错误" + e.getMessage());
            message = new SendMessage(e.getMessage(), false);
        } finally {
            repository.saveAndFlush(message);
        }
    }
}
